package com.example.springboot.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    //    查询所有数据
    @GetMapping("/alldata")
    public Result getData(){
        List<User> all = userService.GetAll();

        return Result.success( userService.GetAll());
    }
    /**
     * 新增用户接口
     */
    @PostMapping
    public Result userRegister(@RequestBody User user){
        user.setRole(user.getRole() != null ? user.getRole() : 2);
        return Result.success(userService.userRegister(user));
    }

    /**
     * 描述：根据ID删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.delete(id);
        return Result.success();
    }

    /**
     * 描述：分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody User search,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        return Result.success(userService.findPage(search, pageNum, pageSize));
    }

    /**
     * 编辑用户信息接口
     */
    @PostMapping("/edit")
    public Result userEdit(@RequestBody User user){

        return Result.success(userService.userEdit(user));

    }

    /**
     * 修改角色信息
     */
    @PostMapping("/changeRole")
    public Result userRole(@RequestBody User user){

        return Result.success(userService.userRole(user));

    }
    /**
     * 描述：通过excel批量导入
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        List<User> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(User.class);
        if (!CollectionUtil.isEmpty(infoList)) {
            for (User info : infoList) {
                try {
                    userService.userRegister(info);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.success();
    }
    /**
     * 描述：批量导出到excel
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<User> all = userService.GetAll();
        List<Map<String, Object>> list = new ArrayList<>(all.size());
        if (CollectionUtil.isEmpty(all)) {
            throw new CustomException("-1", "未查询到数据");
        }
        for (User user : all) {
            Map<String, Object> row = new LinkedHashMap<>();
            row.put("用户名", user.getUserName());
            row.put("性别", user.getGender());
            row.put("年龄", user.getAge());
            row.put("电话", user.getPhone());
            row.put("角色标识", user.getRole());
            row.put("班级Id", user.getGradeId());
            list.add(row);
        }
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=userInfoExcel.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }


    /**
     * 描述：批量删除
     */
    @PostMapping("/delBatch")
    public Result delBatch(@RequestBody List<User> list){
//        后端拿到前端传过来的集合，执行循环删除操作
        for (User user : list) {
//            调用Service层已经写好的删除方法
            userService.delete(user.getId());
        }
        return Result.success();
    }

}
